﻿using System;
using System.Collections.Generic;
using System.IO;
using Android.App;
using Android.Content;
using Android.Content.PM;
using Android.Graphics;
using Android.Graphics.Drawables;
using Android.OS;
using Android.Support.Design.Widget;
using Android.Support.V4.App;
using Android.Support.V4.View;
using Android.Support.V7.App;
using Android.Support.V7.Widget;
using Android.Views;
using Android.Widget;
using Com.Gcssloop.Widget;
using hotfix;
using mb;
using mb.Droid;
using mb.DroidLib;

namespace libmp
{
    public class HomeView
    {
        static ILog logger = ServiceLocator.Instance.Get<ILog>();
        public static MyFragment CreateFragment()
        {
            return new MyFragment()
            {
                Title = "推荐",
                    MyOnCreateView = (f, container) => CreateView(),
            };
        }
        public static View CreateView()
        {
            var ctx = StartActivity.This;
            var layout = new LinearLayout(ctx);
            layout.Orientation = Orientation.Vertical;
            layout.SetBackgroundColor(Util.MainBKColor);

            layout.AddView(CreateTitleBar());
            layout.AddView(CreateMovieView());

            return layout;
        }

        static View CreateMovieView()
        {
            var ctx = StartActivity.This;
            LinearLayout fragContainer = new LinearLayout(ctx);
            fragContainer.Id = View.GenerateViewId();
            fragContainer.LayoutParameters = new ViewGroup.LayoutParams(-1, -1);
            // fragContainer.SetBackgroundColor(Color.Blue);

            var fragMan = ctx.SupportFragmentManager;
            var fragTransaction = fragMan.BeginTransaction();

            var myFrag = Util.FragmentFrom(typeof(HomeMovieView));
            fragTransaction.Add(fragContainer.Id, myFrag, "movie");
            fragTransaction.Commit();

            return fragContainer;
        }

        static void CreateTabBar(ViewGroup parent)
        {
            var ctx = StartActivity.This;
            var tlayout = new LinearLayout(ctx);
            tlayout.LayoutParameters = new LinearLayout.LayoutParams(ViewGroup.LayoutParams.MatchParent, 120);
            parent.AddView(tlayout);

            var inflater = LayoutInflater.From(ctx);
            var ifview = inflater.Inflate(Resource.Layout.htabs_no_ripple, tlayout);
            var tab = tlayout.FindViewById(Resource.Id.sliding_tabs) as TabLayout;
            tab.SetBackgroundColor(Color.Rgb(255, 255, 255));

            var fragments = new List<MyFragment>
            {
                Util.FragmentFrom(typeof(HomeMovieView)),
                Util.FragmentFrom(typeof(HomeAudioView)),
                Util.FragmentFrom(typeof(HomeNovelView)),
            };

            var pager = new ViewPager(ctx);
            pager.Id = View.GenerateViewId();
            pager.Adapter = new MyPagerAdapter(ctx, ctx.SupportFragmentManager) { fragments = fragments };
            pager.LayoutParameters = new LinearLayout.LayoutParams(ViewGroup.LayoutParams.MatchParent, 0, 1);
            pager.OffscreenPageLimit = 3;
            parent.AddView(pager);

            tab.SetupWithViewPager(pager);
            tab.SetSelectedTabIndicatorHeight(8);
            tab.SetTabTextColors(Color.Rgb(96, 96, 96), Color.Rgb(55, 123, 222));
            for (int i = 0; i < tab.TabCount; i++)
            {
                var ot = tab.GetTabAt(i).Text;
                TextView tv;
                tab.GetTabAt(i).SetCustomView(tv = new TextView(ctx)
                {
                    TextSize = 22,
                        Text = ot,
                        TextAlignment = TextAlignment.Center,
                        Gravity = GravityFlags.Center,
                        // Background = new ColorDrawable(Color.Red),
                        LayoutParameters = new ViewGroup.LayoutParams(ViewGroup.LayoutParams.WrapContent, ViewGroup.LayoutParams.WrapContent),
                });
                tv.SetTextColor(Util.MakeColorStateList(Color.Rgb(96, 96, 96), Color.Rgb(55, 123, 222), Util.ClearColor));
            }

        }

        static void triggerRebirth(Context context, Intent nextIntent)
        {
            Intent intent = new Intent(context, typeof(StartActivity));
            intent.AddFlags(ActivityFlags.NewTask);
            intent.PutExtra("KEY_RESTART_INTENT", nextIntent);
            context.StartActivity(intent);
            if (context is Activity)
            {
                ((Activity) context).Finish();
            }
            Java.Lang.JavaSystem.Exit(0);
        }
        static View CreateTitleBar()
        {
            var ctx = StartActivity.This;
            var tlayout = new LinearLayout(ctx);
            tlayout.SetBackgroundColor(Util.MainColor);
            // tlayout.SetBackgroundColor(Color.White);
            tlayout.LayoutParameters = new LinearLayout.LayoutParams(ViewGroup.LayoutParams.MatchParent, 160);
            tlayout.Orientation = Orientation.Horizontal;
            tlayout.SetGravity(GravityFlags.CenterVertical);

            tlayout.AddView(
                Util.MakeImageView("avatar.png", true).SetClickHandler((s, e) =>
                {
                    // if (mb.Util.HasTestFlag == 1)
                    // {
                    //     mb.Droid.Util.ShowMsgBox($"当前服务器:{App.CurrentServerUrl}\n是否退出？", () =>
                    //     {
                    //         triggerRebirth(ctx, null);
                    //     });
                    // }

                    Util.InspectFragment();
                }),
                new LinearLayout.LayoutParams(100, 100, 0) { LeftMargin = 25, });

            LinearLayout searchLayout;
            tlayout.AddView(
                searchLayout = new LinearLayout(ctx)
                {
                    Background = Util.MakeRoundedRect(Color.Rgb(211, 97, 130), 80)
                }.SetGravityEx(GravityFlags.CenterVertical),
                new LinearLayout.LayoutParams(-1, 100, 1) { LeftMargin = 25, RightMargin = 25 });

            searchLayout.AddView(
                new ImageView(ctx).SetImage("search/ask.png"),
                new LinearLayout.LayoutParams(60, ViewGroup.LayoutParams.MatchParent)
                {
                    LeftMargin = 30,
                }
            );

            var inflater = LayoutInflater.From(ctx);
            var infView = inflater.Inflate(Resource.Layout.myedit, searchLayout);
            var searchText = infView.FindViewById(Resource.Id.editText1) as EditText;
            searchText.Hint = "输入搜索内容";
            searchText.SetTextColor(Util.MainBKColor);
            // searchText.SetBackgroundColor(Color.Blue);
            searchText.ImeOptions = Android.Views.InputMethods.ImeAction.Search;
            searchText.EditorAction += (s, e) =>
            {
                if (e.ActionId == Android.Views.InputMethods.ImeAction.Search)
                {
                    e.Handled = true;
                    Util.HideKeyboard(searchText);
                    SearchResultView.OpenSearchResult(searchText.Text);
                }
            };

            return tlayout;
        }
    };

}